<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

class dao_conferences {

    //Methods
    public function __construct() {
        
    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_conferences";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function add($conference_id, $conference_name, $detail, $file_upload) {
        $db = new db_connection();
        $con = $db->open_connect();

        if ($conference_id == 0) {
            $query = "INSERT INTO tbl_conferences(conference_name, detail, file_upload, Addedon) VALUES ('" . $conference_name . "','" . $detail . "','" . $file_upload . "', '" . lib_date::get_now() . "')";
        } else {
            $query = "UPDATE tbl_conferences SET conference_name = '" . $conference_name . "' , detail = '" . $detail . "' , file_upload = '" . $file_upload . "', Addedon = '" . lib_date::get_now() . "' WHERE conference_id = " . $conference_id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($conference_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_conferences WHERE conference_id = " . $conference_id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($conference_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_conferences WHERE conference_id = " . $conference_id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_lastest_ones($number) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_conferences ORDER BY Addedon DESC LIMIT 0, " . $number;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect($con);
        return $list;
    }
    
}
